package com.heima.search.controller;

import com.heima.common.dto.ResponseResult;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApAssociateWords;
import com.heima.search.service.IApAssociateWordsService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 联想词表 前端控制器
 * </p>
 *
 * @author mcm
 * @since 2021-09-10
 */
@RestController
@RequestMapping("/api/v1/associate")
@Api(tags = "联想词表接口")
@CrossOrigin
public class ApAssociateWordsController {

    @Autowired
    private IApAssociateWordsService apAssociateWordsService;
    @Autowired
    private MongoTemplate template;

    /**
     * 查询关联词
     *
     * @param dto
     * @return
     */
    @PostMapping("/search")
    public ResponseResult search(@RequestBody ApArticleSearchDto dto) {
        return apAssociateWordsService.search(dto);
    }

    @GetMapping("/add")
    public ResponseResult add() {
        for (int i = 0; i < 10; i++) {
            ApAssociateWords words = new ApAssociateWords();
            words.setAssociateWords("黑马王子"+i);
            words.setCreatedTime(new Date());
            template.save(words);
        }
        return ResponseResult.okResult();
    }

}
